package com.enqualcomm.kids.activities;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.BuildConfig;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.base.MyHandler;
import com.enqualcomm.kids.bean.StringMessage;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.manager.Controller;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SetMonitorRunnable;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.ResetParams;
import com.enqualcomm.kids.network.socket.request.ResetWatchParams;
import com.enqualcomm.kids.network.socket.request.SearchParams;
import com.enqualcomm.kids.network.socket.request.SetMonitorParams;
import com.enqualcomm.kids.network.socket.request.ShutDownParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminalConfigResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;
import com.enqualcomm.kids.view.ImageUtils;
import com.enqualcomm.kids.view.dialog.CommitDialogCallBack;
import com.enqualcomm.kids.view.dialog.MonitorDialog;
import com.enqualcomm.kids.view.dialog.MyCommitDialog;
import com.enqualcomm.kids.view.dialog.MyPromptDialog;
import com.enqualcomm.kids.view.dialog.ShutDownDialog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.Locale;

import common.utils.MyFrontUtil;
import common.utils.MyLogger;
import common.utils.NetUtil;
import common.utils.PromptUtil;
import de.greenrobot.event.EventBus;

@EActivity(R.layout.activity_terminal_setting)
public class TerminalSettingActivity extends BaseActivity implements View.OnClickListener {
    private static final String SMS_BODY = "挂断电话";
    private static int CHECKBILL_BTN = 1;
    private static int WATCHALARM_BTN = 2;
    /**
     * 上课静默是常驻按钮，有以下几种情况
     * 1、安全区域、语音提醒支持 话费流量手表闹钟在下面显示
     * 2、安全区域，语音提醒其中一个支持 话费流量发在上边，手表闹钟在下边
     * 3、安全区域、语音提醒都不支持 上边就是 上课静默、话费流量、手表闹钟
     */
    private TerminallistResult.Terminal terminal;
    private TerminalConfigResult.Data config;
    private AppDefault appDefault;
    private QueryUserTerminalInfoResult.Data info;

    @ViewById(R.id.checkbill_btn)
    Button checkbill_btn;
    @ViewById(R.id.silence_btn)
    Button silence_btn;

    @ViewById(R.id.alarm_btn)
    View alarm_btn;
    @ViewById(R.id.location_mode_rl)
    RelativeLayout location_mode_rl;
    @ViewById(R.id.volume_rl)
    RelativeLayout volume_rl;
//    @ViewById(R.id.datetime_btn)
//    Button datetime_btn;

    //    @ViewById(R.id.silence_btn)
//    Button silence_btn;
    @ViewById(R.id.alarm_rl)
    RelativeLayout alarm_rl;
    @ViewById(R.id.search_rl)
    RelativeLayout search_rl;
    @ViewById(R.id.fencing_btn)
    Button fencing_btn;


    @ViewById(R.id.icon_iv)
    ImageView icon_iv;
    @ViewById(R.id.name_tv)
    TextView name_tv;
    @ViewById(R.id.number_tv)
    TextView number_tv;
    @ViewById(R.id.title_bar_terminal_icon_iv)
    ImageView title_terminal_icon_iv;
    @ViewById(R.id.title_bar_terminal_name_tv)
    TextView title_terminal_name_tv;
    @ViewById(R.id.alarm_name_tv)
    TextView alarm_name_tv;
    @ViewById(R.id.title_bar_title_tv)
    TextView title_bar_title_tv;
    @ViewById(R.id.number_tv2)
    TextView number_tv2;
    //    @ViewById(R.id.family_btn)
//    Button family_btn;
    @ViewById(R.id.poweroff_track_devide_view)
    View poweroff_track_devide_view;
    @ViewById(R.id.alarm_volume_view)
    View alarm_volume_view;
    @ViewById(R.id.power_reset_track_divide)
    View power_reset_track_divide;
    @ViewById(R.id.loc_watch_line)
    View loc_watch_line;
    @ViewById(R.id.search_loc_line)
    View search_loc_line;
    @ViewById(R.id.power_reset_rl)
    View power_reset_rl;
    @ViewById(R.id.remind_btn)
    Button remind_btn;
    @ViewById(R.id.check_bill_rl)
    RelativeLayout check_bill_rl;
    UserTerminalDefault userTerminalDefault;
    NetworkModel networkModel;
    private PopupWindow mPopupWindow;
    private MonitorDialog monitorDialog;
    private View contentView;
    private Controller mController;

    @AfterViews
    void setupViews() {

        terminal = getTerminal();
        appDefault = new AppDefault();
        TerminalDefault terminalDefault = new TerminalDefault(terminal.terminalid);
        config = terminalDefault.getConfig();

        mController = Controller.getInstance();
        mController.resetTerminalConfig(new TerminalDefault(terminal.terminalid).getConfig());
        userTerminalDefault = new UserTerminalDefault(terminal.userterminalid);
        info = userTerminalDefault.getInfo();
        networkModel = new NetworkModel();
        setupTitleIcon(info, terminal.terminalid, terminal.userterminalid, getString(R.string.t_m_manage_title));

        EventBus eventBus = EventBus.getDefault();
        if (!eventBus.isRegistered(this)) {
            eventBus.register(this);
        }
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (userTerminalDefault != null) {
            init();
        }
    }

    private void refreshFunctions() {
        mController = Controller.getInstance();
        mController.resetTerminalConfig(new TerminalDefault(terminal.terminalid).getConfig());
        init();
    }

    public void onEventMainThread(StringMessage message) {
        switch (message.msg) {
            case StringMessage.INFO_UPDATED://手表资料有改动
                info = userTerminalDefault.getInfo();
                title_terminal_name_tv.setText(info.name);
                name_tv.setText(info.name);
//                number_tv.setText("手表号码： " + info.mobile);
//                number_tv.setText(getString(R.string.pre_select_phone_number) + info.mobile);
                setWatchPhoneNumber(info);
                refreshFunctions();
            case StringMessage.ICON_UPDATED://手表头像有改动
                updateTitle();
                break;
            case StringMessage.REFRESH_TERMINALLIST:
                finish();
                break;
            case StringMessage.OWNER://在家庭成员界面进行移交管理员操作了
                terminal.isowner = 0;
                break;
        }
    }

    @UiThread
    void updateTitle() {
        Bitmap imageBitmap = ImageUtils.getImageBitmap(this, terminal.terminalid, terminal.userterminalid, info.gender);
//        title_terminal_icon_iv.setImageBitmap(imageBitmap);
        icon_iv.setImageBitmap(imageBitmap);
        title_bar_title_tv.setText(getString(R.string.manage_title));
        setupTitleIcon(info, terminal.terminalid, terminal.userterminalid, getString(R.string.t_m_manage_title));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
        EventBus.getDefault().unregister(this);
    }

    private void init() {
        Bitmap imageBitmap = ImageUtils.getImageBitmap(this, terminal.terminalid, terminal.userterminalid, info.gender);

        TextView title_bar_name = (TextView) findViewById(R.id.title_bar_title_tv);//标题
        title_bar_name.setText(getString(R.string.manage_title));
        icon_iv.setImageBitmap(imageBitmap);
        name_tv.setText(info.name);
//        number_tv.setText(getString(R.string.manage_watch_num) + info.mobile);
        setWatchPhoneNumber(info);
        //隐藏手表不具备的功能

        if (!"115.28.144.79".equals(com.android.volley.BuildConfig.IP)) { //热艾服务器不用gps字段
            if (mController.isfence_enable()) {
                fencing_btn.setVisibility(View.VISIBLE);
            } else {
                fencing_btn.setVisibility(View.GONE);
            }
        } else {
            fencing_btn.setVisibility(View.VISIBLE);
        }

        if (mController.isVoice_notice_enable()) {
            remind_btn.setVisibility(View.VISIBLE);
        } else {
            remind_btn.setVisibility(View.GONE);
        }
        if (mController.isOldMainMode()) {
            silence_btn.setVisibility(View.GONE);
        } else {
            silence_btn.setVisibility(View.VISIBLE);
        }

        if (!mController.isCheck_bill_enable()) { //不支持流量话费查询
            checkbill_btn.setVisibility(View.GONE);
            check_bill_rl.setVisibility(View.GONE);
            search_loc_line.setVisibility(View.GONE);
        } else if (getButtonNums(CHECKBILL_BTN) >= 3) {  //支持话费流量，同时也支持安全区域、语音提醒
            checkbill_btn.setVisibility(View.GONE);
            check_bill_rl.setVisibility(View.VISIBLE);
            search_loc_line.setVisibility(View.VISIBLE);
        } else { //支持话费流量，同时安全区域、语音提醒最多有一个支持
            checkbill_btn.setVisibility(View.VISIBLE);
            check_bill_rl.setVisibility(View.GONE);
            search_loc_line.setVisibility(View.GONE);
        }



        if (!mController.isAlarm_enable()) {
            alarm_rl.setVisibility(View.GONE);
            alarm_btn.setVisibility(View.GONE);
        } else if (getButtonNums(WATCHALARM_BTN) >= 3) {
            alarm_rl.setVisibility(View.VISIBLE);
            alarm_btn.setVisibility(View.GONE);
        } else {
            alarm_rl.setVisibility(View.GONE);
            alarm_btn.setVisibility(View.VISIBLE);
        }

        /**手表音量*/
        if (mController.isVolume_enable() && mController.isPet_product_enable()) {
            volume_rl.setVisibility(View.VISIBLE);
//            alarm_name_tv.setText(getString(R.string.manage_pet_alarm));
        } else if (mController.isVolume_enable() && !mController.isPet_product_enable()) {
            volume_rl.setVisibility(View.VISIBLE);
//            alarm_name_tv.setText(getString(R.string.manage_alarm));
        } else {
            volume_rl.setVisibility(View.GONE);
        }

        if (mController.isWork_mode_enable()) {
            if (TextUtils.isEmpty(config.workmode) || "0@0".equals(config.workmode)) {
                location_mode_rl.setVisibility(View.GONE);
            } else {
                location_mode_rl.setVisibility(View.VISIBLE);
            }
        } else {
            location_mode_rl.setVisibility(View.GONE);
        }


        /**
         * 处理亲友圈和寻机功能
         * */
        if (mController.isPet_product_enable()) {
            search_rl.setVisibility(View.GONE);
        } else if (!mController.isPet_product_enable() && mController.isSearch_terminal_enable()) {
            search_rl.setVisibility(View.VISIBLE);
        } else {
            search_rl.setVisibility(View.GONE);
        }

        if (mController.isshutdown_enable()) {
            power_reset_rl.setVisibility(View.VISIBLE);
        } else {
            power_reset_rl.setVisibility(View.GONE);
        }

        /**手表闹钟和音量调节中间的分割线*/
        if (mController.isAlarm_enable() && mController.isVolume_enable() && getButtonNums(WATCHALARM_BTN) >= 3) {
            alarm_volume_view.setVisibility(View.VISIBLE);
        } else {
            alarm_volume_view.setVisibility(View.GONE);
        }

        if (mController.isshutdown_enable() && mController.isSearch_terminal_enable()) {
            poweroff_track_devide_view.setVisibility(View.VISIBLE);
            power_reset_track_divide.setVisibility(View.VISIBLE);
        } else if (mController.isshutdown_enable() && !mController.isSearch_terminal_enable()) {
            power_reset_track_divide.setVisibility(View.GONE);
            poweroff_track_devide_view.setVisibility(View.VISIBLE);
        } else if (!mController.isshutdown_enable() && mController.isSearch_terminal_enable()) {
            power_reset_track_divide.setVisibility(View.VISIBLE);
            poweroff_track_devide_view.setVisibility(View.GONE);
        } else {
            poweroff_track_devide_view.setVisibility(View.GONE);
            power_reset_track_divide.setVisibility(View.GONE);
        }

        if (mController.isAlarm_enable() || mController.isVolume_enable()) {
            loc_watch_line.setVisibility(View.VISIBLE);
        } else {
            loc_watch_line.setVisibility(View.GONE);
        }

    }

    private void setWatchPhoneNumber(QueryUserTerminalInfoResult.Data info) {
//        String mobile_number;
//        String pet_mobile_number;
//        String mobile_number2;
//        String pet_mobile_number2;
//        if (!info.areacode.isEmpty()) {
//            mobile_number = getString(R.string.pre_select_phone_number) + "： +" + info.areacode + info.mobile;
//            pet_mobile_number = getString(R.string.pet_pre_select_phone_number) + "： +" + info.areacode + info.mobile;
//            number_tv.setText(!mController.isPet_product_enable() ? mobile_number : pet_mobile_number);
//        } else {
//            mobile_number = getString(R.string.pre_select_phone_number) + "：" + info.mobile;
//            pet_mobile_number = getString(R.string.pet_pre_select_phone_number) + "： " + info.mobile;
//            number_tv.setText(!mController.isPet_product_enable() ? mobile_number : pet_mobile_number);
//        }
//
//        if (!info.mobile2.isEmpty()) {
//
//            mobile_number = info.areacode.isEmpty() ? getString(R.string.pre_select_phone_number) + " 1：" + info.mobile : getString(R.string.pre_select_phone_number) + " 1： +" + info.areacode + info.mobile;
//            mobile_number2 = info.areacode2.isEmpty() ? getString(R.string.pre_select_phone_number) + " 2：" + info.mobile2 : getString(R.string.pre_select_phone_number) + " 2： +" + info.areacode2 + info.mobile2;
//
//            pet_mobile_number = info.areacode.isEmpty() ? getString(R.string.pet_pre_select_phone_number) + " 1： " + info.areacode + info.mobile : getString(R.string.pet_pre_select_phone_number) + " 1： +" + info.areacode + info.mobile;
//            pet_mobile_number2 = info.areacode2.isEmpty() ? getString(R.string.pet_pre_select_phone_number) + " 2： " + info.areacode2 + info.mobile2 : getString(R.string.pet_pre_select_phone_number) + " 2： +" + info.areacode2 + info.mobile2;
//            number_tv.setText(!mController.isPet_product_enable() ? mobile_number : pet_mobile_number);
//            number_tv2.setText(!mController.isPet_product_enable() ? mobile_number2 : pet_mobile_number2);
//            number_tv2.setVisibility(View.VISIBLE);
//        } else if (info.mobile2.isEmpty()) {
//            number_tv2.setVisibility(View.INVISIBLE);
//        }
        processString(info.name);
    }

    @ViewById(R.id.set_title_bar_right_iv)
    ImageView title_bar_right_iv;

    @Click(R.id.set_title_bar_right_iv)
    void showSetPopWindow() {
        if (contentView == null) {
            contentView = getLayoutInflater().inflate(R.layout.t_setting_pop1, null);
        }
        contentView.findViewById(R.id.pop_endcall_rl).setVisibility(View.GONE);
        contentView.findViewById(R.id.pop_reset_rl).setVisibility(View.VISIBLE);
        contentView.findViewById(R.id.pop_monitor_rl).setOnClickListener(TerminalSettingActivity.this);
        contentView.findViewById(R.id.pop_endcall_rl).setOnClickListener(TerminalSettingActivity.this);
        contentView.findViewById(R.id.pop_reset_rl).setOnClickListener(TerminalSettingActivity.this);

        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.showAsDropDown(title_bar_right_iv);
    }


    @Click(R.id.pop_endcall_rl)
    void dismissWindowAndEndCall() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
        endCall();
    }

    @Click(R.id.pop_reset_rl)
    void dismissWindowAndReset() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
        resetTerminal();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_bar_right_iv://点击右上按钮弹出
                if (contentView == null) {
                    contentView = getLayoutInflater().inflate(R.layout.t_setting_pop1, null);
                }
                if (config.monitor == 1 && terminal.isowner == 1) {//又不让非管理员看到监护按钮，哎
                    contentView.findViewById(R.id.pop_monitor_rl).setVisibility(View.VISIBLE);
                } else {
                    contentView.findViewById(R.id.pop_monitor_rl).setVisibility(View.GONE);
                }
//                if (config.end_call == 1){//去掉挂断电话功能
//                    contentView.findViewById(R.id.pop_endcall_rl).setVisibility(View.VISIBLE);
//                }else {
                contentView.findViewById(R.id.pop_endcall_rl).setVisibility(View.GONE);
//                }
                contentView.findViewById(R.id.pop_reset_rl).setVisibility(View.VISIBLE);
                contentView.findViewById(R.id.pop_monitor_rl).setOnClickListener(TerminalSettingActivity.this);
                contentView.findViewById(R.id.pop_endcall_rl).setOnClickListener(TerminalSettingActivity.this);
                contentView.findViewById(R.id.pop_reset_rl).setOnClickListener(TerminalSettingActivity.this);

                mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                mPopupWindow.setBackgroundDrawable(new ColorDrawable());
                mPopupWindow.showAsDropDown(v);
                break;
            case R.id.pop_monitor_rl:
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                }
                startMonitor();
                break;
            case R.id.pop_endcall_rl:
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                }
                endCall();
                break;
            case R.id.pop_reset_rl:
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                }
                resetTerminal();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        networkModel.onStop();
    }

    @Click(R.id.title_bar_left_iv)
    void finishActivity() {
        finish();
    }

    @Click(R.id.terminal_info_rl)
    void setTerminalInfo() {
        startActivityWithTerminal(TerminalInfoActivity_.class, terminal);
    }

    @Click(R.id.remind_btn)
    void family() {
        if (isKeleSimpleChinese()) {
            new MyPromptDialog(this, getString(R.string.clear_notice), getString(R.string.function_not_support), new CommitDialogCallBack() {
                @Override
                public void onCommit() {

                }

                @Override
                public void onCancel() {

                }
            }).show();
        } else {
            startActivityWithTerminal(VoiceScheduleActivity_.class, terminal);
        }

    }

    @Click(R.id.fencing_btn)
    void fencing() {
        startActivityWithTerminal(FencingActivity.class, terminal);
    }

//    @Click(R.id.silence_btn)
//    void silence() {
//        startActivityWithTerminal(SilenceActivity.class, terminal);
//    }

    @Click(R.id.shutdown_rl)
    void shutdown() {
        if (terminal.isowner != 1) {
            PromptUtil.toast(getApplicationContext(), R.string.only_admin_can_shutdown);
        } else {
            if (config.schpower == 2) {//如果支持远程开机，要在关机时设置一个开机
                new ShutDownDialog(this, info.name, new ShutDownDialog.Listener() {
                    @Override
                    public void schedulePower(String timeStr) {
                        ShutDownParams shutDownParams = new ShutDownParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid, timeStr);
                        networkModel.loadDataFromServer(new SocketRequest<BasicResult>(shutDownParams, null));
                    }

                    @Override
                    public void justShutDown() {
                        ShutDownParams shutDownParams = new ShutDownParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid);
                        networkModel.loadDataFromServer(new SocketRequest<BasicResult>(shutDownParams, null));
                    }
                }).show();
            } else {
                new MyCommitDialog(this, getString(R.string.power_off), getString(R.string.send_power_off_command), info.name, getString(R.string.power_off), getString(R.string.cancel), new CommitDialogCallBack() {
                    @Override
                    public void onCommit() {
                        ShutDownParams shutDownParams = new ShutDownParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid);
                        networkModel.loadDataFromServer(new SocketRequest<BasicResult>(shutDownParams, null));
                    }

                    @Override
                    public void onCancel() {
                    }
                }).show();
            }
        }
    }

    @Click(R.id.power_reset_rl)
    void resetWatch() {//重启手表
        if (terminal.isowner != 1) {
            PromptUtil.toast(getApplicationContext(), R.string.only_admin_can_reset);
        } else {
            new MyCommitDialog(this, getString(R.string.manager_power_reset), getString(R.string.reset_watch_help)
                    , info.name, getString(R.string.reset_watch), getString(R.string.cancel), new CommitDialogCallBack() {
                @Override
                public void onCommit() {
                    ResetWatchParams params = new ResetWatchParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid);
                    networkModel.loadDataFromServer(new SocketRequest<BasicResult>(params, null));
                }

                @Override
                public void onCancel() {
                }
            }).show();
        }
    }

    @Click(R.id.search_rl)
    void search() {
        new MyCommitDialog(this, getString(R.string.search_terminal), getString(R.string.search_watch_help), info.name
                , getString(R.string.search_watch), getString(R.string.cancel), new CommitDialogCallBack() {
            @Override
            public void onCommit() {
                SearchParams searchParams = new SearchParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid);
                networkModel.loadDataFromServer(new SocketRequest<BasicResult>(searchParams, null));
            }

            @Override
            public void onCancel() {
            }
        }).show();
    }

    @Click({R.id.checkbill_btn, R.id.check_bill_rl})
    void checkbill() {
        startActivityWithTerminal(CheckBillActivity.class, terminal);
    }

    void startMonitor() {
        //监护
        SetMonitorParams setMonitorParams = new SetMonitorParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid, appDefault.getUsername());
        if (NetUtil.checkNet(this)) {
//            PromptUtil.longToast(getApplicationContext(),R.string.monitoring);
            monitorDialog = new MonitorDialog(this);
            monitorDialog.show();
            NetUtil.execute(new SetMonitorRunnable(mHandler, setMonitorParams, info));
        } else {
            PromptUtil.toast(getApplicationContext(), R.string.app_no_connection);
        }
    }

    @Click(R.id.silence_btn)
    void silence() {
        startActivityWithTerminal(SilenceActivity.class, terminal);

    }

    MyHandler mHandler = new MyHandler(this);

    @Override
    protected void handleMessage(Message msg) {
        switch (msg.what) {
            case SetMonitorRunnable.OK:
                if (monitorDialog != null) {
                    monitorDialog.dismiss();
                    monitorDialog = null;
                }
                String number = (String) msg.obj;
                dial(number);
                break;
            case SetMonitorRunnable.POWER_OFF:
                if (monitorDialog != null) {
                    monitorDialog.setErrorMsg(R.string.cannot_monitor_by_power_off);
                    mHandler.sendEmptyMessageDelayed(100, 2000);//关掉对话框
                }
//                PromptUtil.toast(getApplicationContext(), R.string.cannot_monitor_by_power_off);
                break;
            case SetMonitorRunnable.ERROR:
                if (monitorDialog != null) {
                    monitorDialog.setErrorMsg(R.string.monitor_failure);
                    mHandler.sendEmptyMessageDelayed(100, 2000);//关掉对话框
                }
//                PromptUtil.toast(getApplicationContext(), R.string.monitor_failure);
                break;
            case 100:
                if (monitorDialog != null) {
                    monitorDialog.dismiss();
                    monitorDialog = null;
                }
                break;
        }
    }

    private void dial(String telnumber) {
        Uri uri = null;
        if (telnumber.startsWith("00")) {
            uri = Uri.parse("tel:+" + telnumber.substring(2, telnumber.length()));
        } else {
            uri = Uri.parse("tel:" + telnumber);
        }

        Intent tellPhone = new Intent(Intent.ACTION_DIAL, uri);
        try {
            startActivity(tellPhone);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void endCall() {
//        startActivityWithTerminal(EndCallActivity.class,terminal);
        new MyCommitDialog(this, getString(R.string.end_call), getString(R.string.end_call_jjww),
                "停止", new CommitDialogCallBack() {
            @Override
            public void onCommit() {
                try {
                    Uri uri = Uri.parse("smsto:" + info.mobile);
                    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);//ACTION_SENDTO
                    sendIntent.putExtra("sms_body", SMS_BODY);
                    startActivity(sendIntent);
                } catch (Exception e) {
                }
            }

            @Override
            public void onCancel() {
            }
        }).show();
    }

    void resetTerminal() {
        if (terminal.isowner != 1) {
            PromptUtil.toast(getApplicationContext(), R.string.only_admin_can_clearup);
        } else {
            new MyCommitDialog(this, getString(R.string.one_key_clean), getString(R.string.clean_dialog_desc),
                    getString(R.string.clean),
                    new CommitDialogCallBack() {
                        @Override
                        public void onCommit() {
                            new MyCommitDialog(TerminalSettingActivity.this, getString(R.string.one_key_clean),
                                    getString(R.string.clean_resure), new CommitDialogCallBack() {
                                @Override
                                public void onCommit() {
                                    networkModel.loadDataFromServer(new SocketRequest<BasicResult>(new ResetParams(appDefault.getUserkey(), appDefault.getUserid(), terminal.terminalid),
                                            new NetworkListener<BasicResult>() {
                                                @Override
                                                public void onError(VolleyError error) {
                                                    PromptUtil.toast(getApplicationContext(), R.string.app_no_connection);
                                                }

                                                @Override
                                                public void onSuccess(BasicResult response) {
                                                    if (response.code == 0) {
                                                        EventBus.getDefault().post(new StringMessage(StringMessage.REFRESH_TERMINALLIST));
                                                        // 回到主页面,重新获取列表
                                                        finish();
                                                    }
                                                }
                                            }));
                                }

                                @Override
                                public void onCancel() {
                                }
                            }).show();
                        }

                        @Override
                        public void onCancel() {
                        }
                    }).show();
        }
    }

//    @Click(R.id.detach_notice_btn)
//    void detachNotice() {
//        startActivityWithTerminal(DetachNoticeActivity.class, terminal);
//    }

    @Click(R.id.location_mode_rl)
    void locationMode() {
        startActivityWithTerminal(LocationModeActivity.class, terminal);
    }

    @Click({R.id.alarm_rl, R.id.alarm_btn})
    void watchAlarm() {
        startActivityWithTerminal(AlarmActivity.class, terminal);
    }

    @Click(R.id.volume_rl)
    void watchVolume() {
        startActivityWithTerminal(WatchVolumeActivity.class, terminal);
    }

//    @Click(R.id.datetime_btn)
//    void watchDatatime() {
//        startActivityWithTerminal(SetWatchTimeActivity.class, terminal);
//    }

    //判斷手機語言當前版本是不是簡體中文
    private boolean isKeleSimpleChinese() {
        return (!"CN".equals(Locale.getDefault().getCountry().toUpperCase(Locale.getDefault()))) && "科乐-科乐+".equals(BuildConfig.CHANNEL);
    }

    private int getButtonNums(int judgeButton) {
        int buttons = 0;
        if (mController.isfence_enable()) {
            buttons++;
        }
        if (mController.isVoice_notice_enable()) {
            buttons++;
        }
        if (!mController.isOldMainMode()) {
            buttons++;
        }
        if (judgeButton == CHECKBILL_BTN) {
            return buttons;
        } else {
            return mController.isCheck_bill_enable() ? ++buttons : buttons;
        }
    }


    @ViewById(R.id.m_manage_remote_off_tv)
    TextView m_manage_remote_off_tv;
    @ViewById(R.id.m_manage_remote_reboot_tv)
    TextView m_manage_remote_reboot_tv;
    @ViewById(R.id.m_manage_track_tv)
    TextView m_manage_track_tv;
    @ViewById(R.id.m_manage_location_mode_tv)
    TextView m_manage_location_mode_tv;
    @ViewById(R.id.m_manage_bill_tv)
    TextView m_manage_bill_tv;
    @ViewById(R.id.m_manage_volume_tv)
    TextView m_manage_volume_tv;

    /**
     * 按照领导的意思修改字符串拼接
     */
    private void processString(String watchName) {

        MyFrontUtil.initTextViewText(this, name_tv, watchName, getString(R.string.t_m_watch_info));
        /**远程关机*/
        MyFrontUtil.initTextViewText(this, m_manage_remote_off_tv, watchName, getString(R.string.t_m_off_begin)
                , getString(R.string.t_m_off_end), 2, watchName.length() + 2);
        /**远程重启*/
        MyFrontUtil.initTextViewText(this, m_manage_remote_reboot_tv, watchName, getString(R.string.t_m_reboot_begin)
                , getString(R.string.t_m_reboot_end), 2, watchName.length() + 2);
        /**寻找手表*/
        MyFrontUtil.initTextViewText(this, m_manage_track_tv, watchName, getString(R.string.t_m_track_begin)
                , getString(R.string.t_m_track_end), 2, watchName.length() + 2);
        /**手表定位模式*/
        MyFrontUtil.initTextViewText(this, m_manage_location_mode_tv, watchName, getString(R.string.t_m_location_mode));
        /**话费流量*/
        MyFrontUtil.initTextViewText(this, m_manage_bill_tv, watchName, getString(R.string.t_m_bill));
        /**手机音量*/
        MyFrontUtil.initTextViewText(this, m_manage_volume_tv, watchName, getString(R.string.t_m_volume));
        /**手表闹钟*/
        MyFrontUtil.initTextViewText(this, alarm_name_tv, watchName, getString(R.string.t_m_alarm));
    }

}
